/*
	FileName : JT_C_FLASH_PARA.c
	Description :
		Manage the format of JT_C Recording CPU flash parameters.
	
	Created on 2017.11.11 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/



#include "JT_C_FLASH_PARA.h"
#include "MiscFnx.h"		/// for CRC function
#include "F205_Flash.h"		/// 

//----------------------------------------------------------------



uint8_t ParamBuf[256];

//-----------------------------------



uint8_t Para_readAllFromFlash(void)
{
	return F205_ReadParams(PARAMS_ADDRESS, &(ParamBuf[0]), sizeof(ParamBuf));;
}

uint8_t Para_writeAllToFlash(void)
{
	return F205_WriteParams(PARAMS_ADDRESS, &(ParamBuf[0]), sizeof(ParamBuf));
}

uint8_t Para_setDevID(uint8_t *dev)
{
	uint16_t crc;
	
	ParamBuf[53] = dev[0];
	ParamBuf[54] = dev[1];
	
	crc = Calc_CRCByte(ParamBuf + 32, 126);
	ParamBuf[158] = crc >> 8;
	ParamBuf[159] = crc & 0xff;
			
	return Para_writeAllToFlash();
}

uint16_t Para_getDevID(void)
{
	return (ParamBuf[53] + ((uint16_t)ParamBuf[54] << 8));
}

void Para_getBoardID(uint8_t *readBuf)
{
	uint8_t i;
	for (i = 0; i < 8; i++)
		readBuf[i] = ParamBuf[50 + i];
	return;
}
uint8_t Para_setBoardID(uint8_t *writeBuf)
{
	uint16_t crc;
	uint8_t i;
	for (i = 0; i < 8; i++)
		ParamBuf[50 + i] = writeBuf[i];
	
	crc = Calc_CRCByte(ParamBuf + 32, 126);
	ParamBuf[158] = crc >> 8;
	ParamBuf[159] = crc & 0xff;
				
	return Para_writeAllToFlash();
}


uint8_t Para_getMaintInfo(uint8_t *readBuf)
{	
	uint8_t i, n;
	
	n = ParamBuf[44];
	
	if(n > 5)
		n = 5;
	
	for (i = 0; i < n * 20; i++)
		readBuf[i] = ParamBuf[58 + i];
	
	return n*20;
}


uint8_t Para_setMaintInfo(uint8_t *writeBuf)
{
	uint16_t i, len, crc;
	
	if (ParamBuf[45] > 4)
		ParamBuf[45] = 0;

	len = ParamBuf[45] * 20 + 58;
	for (i = 0; i < 20; i++)
		ParamBuf[len + i] = writeBuf[i];
	
	ParamBuf[44]++;
	if (ParamBuf[44] > 5)
		ParamBuf[44] = 5;
	
	ParamBuf[45]++;
	if (ParamBuf[45] > 4)
		ParamBuf[45] = 0;
	
	crc = Calc_CRCByte(ParamBuf + 32, 126);
	ParamBuf[158] = crc >> 8;
	ParamBuf[159] = crc & 0xff;
				
	return Para_writeAllToFlash();
}

uint8_t Para_AdcChParasCheck(void)
{
	uint16_t crc, tmp;
	
	crc = Calc_CRCByte(ParamBuf + 32, 126);
	tmp = ((uint16_t)ParamBuf[158] << 8) + ParamBuf[159];
	
	if ((crc != tmp) || \
		(ParamBuf[32] != 0x52) || \
		(ParamBuf[33] != 0x47) || \
		(ParamBuf[34] != 0x65) || \
		(ParamBuf[35] != PARA_ADC_AMRS_ID4))
	{
		return 1;
	}
	
	return 0;
}
uint8_t Para_setAdcChParasDefault(void)
{
	uint8_t i;
	uint16_t crc, tmp;
	
	ParamBuf[32] = 0x52;
	ParamBuf[33] = 0x47;
	ParamBuf[34] = 0x65;
	ParamBuf[35] = PARA_ADC_AMRS_ID4;
	
	tmp = DEFAULT_SIGNAL_AC_OFFSET;
	crc = DEFAULT_SIGNAL_AC_RATIO;
	
	for (i = 0; i < 2; i++){
		
		ParamBuf[36 + (i << 2)] = tmp & 0xff;
		ParamBuf[36 + (i << 2) + 1] = tmp >> 8;
		
		ParamBuf[36 + (i << 2) + 2] = crc & 0xff;
		ParamBuf[36 + (i << 2) + 3] = crc >> 8;
	}
	
	for (i = 44; i < 158; i++)
		ParamBuf[i] = 0;
	
	crc = Calc_CRCByte(ParamBuf + 32, 126);
	ParamBuf[158] = crc >> 8;
	ParamBuf[159] = crc & 0xff;

	return Para_writeAllToFlash();
}

uint8_t Para_setAdcChParas(uint8_t *writeBuf)
{
	uint16_t i, temp;
	
	for (i = 0; i < 2; i++){
						
		temp = ((uint16_t)writeBuf[(i << 2) + 1] << 8) + writeBuf[(i << 2) + 0];
		ParamBuf[36 + (i << 2) + 0] = temp & 0xff;
		ParamBuf[36 + (i << 2) + 1] = temp >> 8;
				
		temp = ((uint16_t)writeBuf[(i << 2) + 3] << 8) + writeBuf[(i << 2) + 2];
		ParamBuf[36 + (i << 2) + 2] = temp & 0xff;
		ParamBuf[36 + (i << 2) + 3] = temp >> 8;
	}
	
	temp = Calc_CRCByte(ParamBuf + 32, 126);
	ParamBuf[32 + 126] = temp >> 8;
	ParamBuf[33 + 126] = temp & 0xff;
				
	return Para_writeAllToFlash();
}

void Para_getAdcChParas(uint16_t *offset,uint16_t *ratio_int)
{
	uint16_t i, temp;
	
	for (i = 0; i < 2; i++){
						
		temp = ((uint16_t)ParamBuf[36 + (i << 2) + 1] << 8) + ParamBuf[36 + (i << 2) + 0];
		offset[i] = temp;
					
		temp = ((uint16_t)ParamBuf[36 + (i << 2) + 3] << 8) + ParamBuf[36 + (i << 2) + 2];
		ratio_int[i] = temp;
	
	}
	return;
}

uint8_t* Para_getCFIDAddr(void)
{
	return ParamBuf+ 50;
}

//-------------------------------------------------------------------------
//                      version
//-------------------------------------------------------------------------
uint8_t Para_versionCheck(void)
{
	
	uint16_t crc, tmp;
	
	crc = Calc_CRCByte(ParamBuf + 16, 14);
	tmp = ((uint16_t)ParamBuf[30] << 8) + ParamBuf[31];
	
	if ((crc != tmp) || \
		(ParamBuf[16] != 0x71) || \
		(ParamBuf[17] != 0x61) || \
		(ParamBuf[18] != 0x42) || \
		(ParamBuf[19] != 0x55) || \
		(ParamBuf[20] != SOFT_VER_MAIN) || \
		(ParamBuf[21] != SOFT_VER_SUB) || \
		(ParamBuf[22] != 0x20) || \
		(ParamBuf[23] != SOFT_VER_YEAR) || \
		(ParamBuf[24] != SOFT_VER_MONTH) || \
		(ParamBuf[25] != SOFT_VER_DATE)  || \
		(ParamBuf[26] != SOFT_VER_BUILD)  || \
		(ParamBuf[27] != SOFT_VER_SUBBUILD) 
	)
	{
		return 1;
	}
	return 0;
}

uint8_t Para_setVersion(void)
{
	uint16_t crc;
	
	ParamBuf[16] = 0x71;
	ParamBuf[17] = 0x61;
	ParamBuf[18] = 0x42;
	ParamBuf[19] = 0x55;

	ParamBuf[20] = SOFT_VER_MAIN;
	ParamBuf[21] = SOFT_VER_SUB;
	ParamBuf[22] = 0x20;
	ParamBuf[23] = SOFT_VER_YEAR;
	ParamBuf[24] = SOFT_VER_MONTH;
	ParamBuf[25] = SOFT_VER_DATE;

	ParamBuf[26] = SOFT_VER_BUILD;
	ParamBuf[27] = SOFT_VER_SUBBUILD;
	
	crc = Calc_CRCByte(ParamBuf + 16, 14);
	ParamBuf[30] = crc >> 8;
	ParamBuf[31] = crc & 0xff;
	
	return Para_writeAllToFlash();
}

void Para_getBiosVersion(uint8_t *readBuf)
{
	uint8_t i;
	readBuf[0] = 0;///
	for(i = 0; i < 8; i++)
	{
		readBuf[1 + i] = ParamBuf[4 + i];
	}
	return;
}

void Para_getAppVersion(uint8_t *readBuf)
{
	uint8_t i;
	readBuf[0] = 1;///
	for(i = 0; i < 8; i++)
	{
		readBuf[1 + i] = ParamBuf[20 + i];
	}
	return;
}

void Para_getUsbVersion(uint8_t *readBuf)
{
	uint8_t i;
	
	readBuf[0] = 2;
	for (i = 0; i < 8; i++){
		
		readBuf[1 + i] = ParamBuf[164 + i];
	}
	return;
}

///EOF
